<?php
class CategoryController extends \BaseController {


    public function getManagement() {

        $Category = new Category;
        $categories = $Category->getCategories(1);
        $categoriesHtmlSelect = $Category->getCategories(1, true);

        $categoriesHtmlUl = $this->renderCategoriesList($categories);

        return View::make('front/category_management')
            ->with('categoriesHtmlUl', $categoriesHtmlUl)
            ->with('categoriesHtmlSelect', $categoriesHtmlSelect)
            ->with('action', 'add');
    }


    public function postAdd() {
        if(!Auth::check()) {
            return 'not logined!';
        }

        $inputData = Input::all();

        $Category = new Category;

        $Category->p_id = $inputData['p_id'];
        $Category->name = $inputData['name'];
        $Category->description = $inputData['description'];
        $Category->user_id = Auth::user()->id;

        $Category->save();

        return Redirect::to('/categories/management')
            ->with('message','添加成功！');
    }

    public function getDelete($id) {
        if(!Auth::check()) {
            return 'not logined!';
        }

        $Category = Category::find($id);
        $Category->delete();

        return Redirect::to('/categories/management')
            ->with('message', '删除成功！');
    }

    public function getEdit($id) {


        $Category = new Category;
        $currentCategory = Category::find($id);
        $categories = $Category->getCategories(1);
        $categoriesHtmlSelect = $Category->getCategories(1, true, '', $currentCategory->p_id);

        $categoriesHtmlUl = $this->renderCategoriesList($categories);


        return View::make('front/category_management')
            ->with('currentCategory', $currentCategory)
            ->with('categoriesHtmlUl', $categoriesHtmlUl)
            ->with('categoriesHtmlSelect', $categoriesHtmlSelect)
            ->with('action', 'edit');





    }

    public function postEdit() {
        if(!Auth::check()) {
            return 'not logined!';
        }

        $inputData = Input::all();

        $Category = Category::find($inputData['category_id']);
        $Category->name = $inputData['name'];
        $Category->description = $inputData['description'];
        $Category->p_id = $inputData['p_id'];

        $Category->save();

        return Redirect::to('/categories/management')
            ->with('message', '修改成功！');

    }

    public function renderCategoriesList($categories) {

        $html = '<ul>';
        for($i = 0; $i < count($categories); $i++) {
            $html .= "<li><span>". $categories[$i]->name . "</span>".
                        "<a href='/categories/edit/". $categories[$i]->id ."'>".
                            "<i class='glyphicon glyphicon-edit'></i>".
                        "</a>".
                        "<a href='/categories/delete/". $categories[$i]->id ."'>".
                            "<i class='glyphicon glyphicon-trash'></i>".
                        "</a>";
            if(count($categories[$i]->subs) == 0) {

            }else {
                $html .= $this->renderCategoriesList($categories[$i]->subs);
            }
            $html .= "</li>";
        }
        $html .= '</ul>';
        return $html;


    }

}
